﻿using System;
using System.Collections.Generic;
using Library.Persistence.Cloud;
using NHibernate;
using Cloud.BaseCode;

namespace Cloud.Cloud
{
    public partial class KullaniciAjanda : BasePage
    {
        protected void Kaydet(object sender, EventArgs e)
        {
            CloudFirmaKullaniciAjanda dto;
            if (!String.IsNullOrEmpty(Request.Form["ajandaId"]) && Request.Form["ajandaId"] != "undefined")
                dto = CloudDao().CloudFirmaKullaniciAjanda().Get(Convert.ToInt32(Request.Form["ajandaId"]), LockMode.Upgrade);
            else
                dto = new CloudFirmaKullaniciAjanda();

            dto.Aciklama = Request.Form["Aciklama"];
            dto.BaslangicTarih = Request.Form["BaslangicTarihi"];
            dto.BaslangicSaat = Request.Form["BaslangicSaati"];
            dto.BitisTarih = Request.Form["BitisTarihi"];
            dto.BitisSaat = Request.Form["BitisSaati"];
            dto.KullaniciId = Kullanici().FirmaKullaniciId;

            if (!String.IsNullOrEmpty(Request.Form["ajandaId"]) && Request.Form["ajandaId"] != "undefined")
                CloudDao().CloudFirmaKullaniciAjanda().Update(dto);
            else
                CloudDao().CloudFirmaKullaniciAjanda().Insert(dto);
            Commit();
        }

        protected void Sil(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(Request.Form["ajandaId"]) || Request.Form["ajandaId"] == "undefined") 
                return;
            CloudDao().CloudFirmaKullaniciAjanda().Delete(Convert.ToInt32(Request.Form["ajandaId"]));
            Commit();
        }

        protected void Json(object sender, EventArgs e)
        {
            Response.ContentType = "text/plain";
            Response.Expires = -1;            
            var dtoAjandaList = CloudDao().CloudFirmaKullaniciAjanda().FindByKullaniciIdAndTarih(Kullanici().FirmaKullaniciId.ToString(), FromNixDate(GetQueryString("start")), FromNixDate(GetQueryString("end")), LockMode.Read);
            IList<CalendarDto> tasksList = new List<CalendarDto>();
            foreach(var dtoAjanda in dtoAjandaList)
            {
                try
                {
                    var bas = Convert.ToDateTime(dtoAjanda.BaslangicTarih)
                        .AddHours(Convert.ToInt32(dtoAjanda.BaslangicSaat.Split(':')[0]))
                        .AddMinutes(Convert.ToInt32(dtoAjanda.BaslangicSaat.Split(':')[1]));
                    var bit = Convert.ToDateTime(dtoAjanda.BitisTarih)
                        .AddHours(Convert.ToInt32(dtoAjanda.BitisSaat.Split(':')[0]))
                        .AddMinutes(Convert.ToInt32(dtoAjanda.BitisSaat.Split(':')[1]));

                    tasksList.Add(new CalendarDto
                                      {
                                          id = dtoAjanda.AjandaId,
                                          title = dtoAjanda.Aciklama,
                                          start = ToJsDate(bas),
                                          end = ToJsDate(bit),
                                          allDay = false                                          
                                      });
                } catch {}
            }
            var oSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();            
            Response.Write(oSerializer.Serialize(tasksList));
            Response.End();
        }

        public class CalendarDto
        {
            public int id { get; set; }
            public string title { get; set; }
            public string start { get; set; }
            public string end { get; set; }
            public string url { get; set; }
            public bool allDay { get; set; }            
        }

        private static string ToJsDate(DateTime date)
        {
            return date.Year + "-" + (date.Month < 10 ? "0" + date.Month : date.Month.ToString()) + "-" + (date.Day < 10 ? "0" + date.Day : date.Day.ToString()) + " " + (date.Hour < 10 ? "0" + date.Hour : date.Hour.ToString()) + ":" + (date.Minute < 10 ? "0" + date.Minute : date.Minute.ToString()) + ":00";
        }

        private static string FromNixDate(string nixDate)
        {
            var origin = new DateTime(1970, 1, 1, 0, 0, 0, 0);
            return origin.AddSeconds(Convert.ToDouble(nixDate)).ToShortDateString();
        }
    }
}